package com.uchengwang.ui.cm_ui.umsg.http;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.cm_ui.umsg.bean.ClubListBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.GroupBean;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/7/15.
 */

public class ClubHttpManager {
    private static ClubHttpManager manager;
    private ClubHttpManager (){}

    public static ClubHttpManager getManager() {
        if (manager == null){
            manager = new ClubHttpManager();
        }
        return manager;
    }

    /**
     * 是否在兴趣圈/老乡会校吧里创建过群组
     * */
    public void isCreateGroupInBa(Activity activity, final int orgType, final CreateOrNotListener listener){
        HttpManager.newInstance(activity).query(UserConfig.ChackMyGroup.method, UserConfig.ChackMyGroup.URL, null, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                listener.create(status, false);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status !=0){
                    listener.create(status, false);
                    return;
                }

                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<GroupBean> beans = JsonUtils.getManager().parseList(data, GroupBean.class);
                    if (beans != null && beans.size() >0){
                        for (GroupBean b: beans){
                            if (b.getOrgType()== orgType){
                                listener.create(0, true);
                                return;
                            }
                        }
                        listener.create(status, false);
                    }else {
                        listener.create(status, false);
                    }
                }else {
                    listener.create(status, false);
                }
            }
        });
    }

    /**
     * 社团是否在某校吧里关联过
     * @param baId 校吧ID
     * */
    public void isClubLinkInBa(Activity activity, final long baId, final CreateOrNotListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ORG_TYPE, BaseConfig.TYPE_CLUB);
        map.put(BaseConfig.CHECK_UID, baId);
        getLink(activity, map, new GetLinkListener() {
            @Override
            public void onResult(int stutas, List<SchoolClubBean> list) {
                if (list != null && list.size() >0){
                    for (SchoolClubBean b: list){
                        if (b.getUid()== UCommonUtil.getUtil().getId()){
                            listener.create(stutas, true);
                            return;
                        }
                    }
                    listener.create(stutas, false);
                }else {
                    listener.create(stutas, false);
                }
            }
        });

    }

    /**
     * 是否社团关联过兴趣圈校吧
     * @param clubId 社团ID
     * @param baId 校吧ID
     * */
    public void isClubLink(Activity activity, final long clubId, final long baId, final LinkListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ORG_TYPE, BaseConfig.TYPE_SCHOOL_INTEREST);
        map.put(BaseConfig.CHECK_UID, clubId);

        getLink(activity, map, new GetLinkListener() {
            @Override
            public void onResult(int stutas, List<SchoolClubBean> list) {
                if (list != null && list.size() >0){
                    for (SchoolClubBean b: list){
                        if (b.getUid()== baId){
                            listener.onResult(stutas, true, true);
                            return;
                        }
                    }
                    listener.onResult(stutas, true, false);
                }else {
                    listener.onResult(stutas, false, false);
                }
            }
        });
    }

    /**
     * 社团获取关联兴趣圈/兴趣圈获取关联社团
     * */
    public void getLink(Activity activity, MapRequest map, final GetLinkListener linkListener){
        HttpManager.newInstance(activity).query(ClubConfig.ClubLink.method_get, ClubConfig.ClubLink.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                linkListener.onResult(status, null);
            }
            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (status !=0){
                    linkListener.onResult(status, null);
                    return;
                }

                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<SchoolClubBean> list = JsonUtils.getManager().parseList(data, SchoolClubBean.class);
                    if (list != null && list.size() >0){
                        linkListener.onResult(status, list);
                    }else {
                        linkListener.onResult(status, null);
                    }
                }else {
                    linkListener.onResult(status, null);
                }
            }
        });
    }

    /**
     * 社团关联校吧
     * @param id 校吧ID
     * @param isLink 是否关联 true 已关联
     * */
    public void link(final Activity activity, long id, final boolean isLink, final CreateOrNotListener listener){
        LoadingUtils.getUtils().loading(activity);
        IHttpManager.RequestMethod method = null;
        MapRequest map = new MapRequest();
        if (isLink){
            //关注了 就取消
            method = ClubConfig.ClubLink.method_delete;
            map.put(BaseConfig.ORG_ID, id);
        }else {
            //没关注 就关注
            method = ClubConfig.ClubLink.method_post;
            map.put(BaseConfig.ID, id);
        }
        HttpManager.newInstance(activity).query(method, ClubConfig.ClubLink.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "status="+status+"=msg="+msg);
                listener.create(status, false);
                UCommonUtil.getUtil().showToast(activity, "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                if (status != 0){
                    listener.create(status, false);
                    return;
                }
                //关联/取消关联 成功
                listener.create(status, true);
            }
        });

    }

    /**
     * 用户同意加入一个社团
     * @param id 邀请ID
     * */
    public void acceptClubInvite(final Activity activity, long id, HttpCallBack callBack){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, id);
        HttpManager.newInstance(activity).query(ClubConfig.Invite.method, ClubConfig.Invite.URL_ACCEPT, map, callBack);
    }


    /**
     * 获取社团信息
     * */
    public void getClubInfo (Activity activity, long clubUid, final OnHttpResultSingleListener<SchoolClubBean> listener) {
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, clubUid);

        HttpManager.newInstance(activity).query(ClubConfig.Club.method, ClubConfig.Club.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status != 0) {
                    listener.onResult(status, msg, null);
                    return;
                }
                if (status == 0 && !TextUtils.isEmpty(data)){
                    SchoolClubBean bean = JsonUtils.getManager().stringToBean(data, SchoolClubBean.class);
                    listener.onResult(status, msg, bean);
                }else {
                    listener.onResult(status, msg, null);
                }
            }
        });
    }

    /**
     * 获取本人创建的所有社团
     * */
    public void getMyClubs(final Activity activity, final OnHttpResultListener<SchoolClubBean> listener){
        if (!UCommonUtil.getUtil().isLogin()){
            return;
        }

        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, 1);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(activity).query(UserConfig.MyClubs.method, UserConfig.MyClubs.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);

            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg+"=data="+data);
                if (status == 0 && !TextUtils.isEmpty(data)){
                    ClubListBean bean = JsonUtils.getManager().stringToBean(data, ClubListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(),bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }


    public interface CreateOrNotListener{
        void create(int stutas, boolean isCreate);
    }

    public interface GetLinkListener{
        void onResult(int stutas, List<SchoolClubBean> list);
    }

    //isCreate整个兴趣圈有没有关联过  isCreateInThis某个校吧有没有关联过
    public interface LinkListener{
        void onResult(int stutas, boolean isCreate, boolean isCreateInThis);
    }
}
